import {defineStore} from 'pinia';

interface AppProcess {
    pid?: number;
    name: string;
}

export const useSpawnPidStore = defineStore('spawnPid', {
        state: () => (
            {processes: [] as AppProcess[]}
        ),
        getters: {
            getSpawnPid: (state) => { // ✅ 先接收 state
                return (appName: string) => { // ✅ 返回带参数的函数
                    return state.processes.find(item => item.name === appName)?.pid
                }
            }
        },
        actions: {
            updateSpawnPid(spawnPid: number, appName: string) {
                const app = this.processes
                    .find(item => item.name === appName);
                if (app) {
                    app.pid = spawnPid;
                } else {
                    this.processes.push({
                        pid: spawnPid,
                        name: appName,
                    });
                }
            },
            removeSpawnPid(appName: string) {
                const index = this.processes.findIndex(item => item.name === appName);
                if (index !== -1){
                    this.processes.splice(index, 1);
                }
            }
        },
    }
);